import React, { Component } from 'react';
import { BrowserRouter, Route, Link, withRouter } from 'react-router-dom';
import './login.css';
import login from '../../../api/login/login'
import cookie from 'react-cookies';
// 输入框
import { List, InputItem, WhiteSpace } from 'antd-mobile';
import { createForm } from 'rc-form';

import { Button, WingBlank } from 'antd-mobile';

class BasicInputExample extends Component {
    constructor() {
        super()
        this.state = {
            code: 0,
            userName: '',
            pwd: ''
        }
    }
    changeName = (value) => {
        this.setState({
            userName: value
        })
    }
    changePwd = (value) => {
        this.setState({
            pwd: value
        })
    }
    submit() {
        this.props.methods(this.state)
        let msg = {
            username: this.state.userName,
            password: this.state.pwd
        }
        setTimeout(() => {
            login(msg)
                .then(res => {
                    console.log(res)
                    console.log("this", this.state)
                    if (res.data.code === 0) {
                        alert("请正确填写用户名密码")
                        console.log(this, 111, this.state)
                    } else {
                        this.setState({
                            code: 1,
                        })
                        cookie.save('uid', res.data.data.uid, 30)
                        cookie.save('username', res.data.data.usn, 30)
                        cookie.save('pwd', this.state.pwd, 30)
                    }
                    if (this.state.code === 1) {
                        this.props.history.push("/home/success")
                    }
                })
            
        }, 1000)
    }
    render() {
        const { getFieldProps } = this.props.form;
        return (
            <div>
                <List renderHeader={() => ''}>
                    <InputItem
                        {...getFieldProps('userName')}
                        clear
                        placeholder="请输入账号"
                        onChange={this.changeName}
                        value={this.state.userName}
                    >账号:</InputItem>
                    <InputItem
                        {...getFieldProps('passWord')}
                        clear
                        type="password"
                        placeholder="请输入密码"
                        onChange={this.changePwd}
                        value={this.state.pwd}
                    >密码:</InputItem>
                </List>
                <WhiteSpace />
                <WingBlank>
                    {/* <Link to="/home/" role="button" className="am-button" aria-disabled="false" type="primary" onClick={this.submit.bind(this)} style={{ backgroundColor: "rgb(16,142,233)", color: "white" }}>登录</Link> */}
                    <Button type="primary" onClick={this.submit.bind(this)}>登录</Button>
                    <WhiteSpace />
                </WingBlank>
            </div>
        );
    }
}

const BasicInputExampleWrapper = createForm()(BasicInputExample);

export default withRouter(BasicInputExampleWrapper)